package com.zhangql.frame;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import android.app.ListActivity;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.SimpleAdapter;

import com.zhangql.R;
import com.zhangql.util.ActivityStack;

public class sportRecord extends ListActivity{
   
	
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		show();
		
		
		ActivityStack.getInstance().addActivity(this);
	}
	
	private void show(){
		SharedPreferences sp = getSharedPreferences("sportData", MODE_WORLD_WRITEABLE);
		Map<String, String> dataMap = (Map<String, String>) sp.getAll();
		
		//{20120816153745=costTime:2;kaluli:0.15;Type:onfoot}
		
		List<Map<String,Object>> list = new ArrayList<Map<String,Object>>();
		
		Map<String,Object>  map = null;
		
		for(Iterator iterator = dataMap.keySet().iterator();iterator.hasNext();){
			String key = (String)iterator.next();
			String value = (String)dataMap.get(key);
			
			String type = "";
			String costTime = "";
			
			StringBuffer buffer = new StringBuffer();
			//2012 08 30 12 12 12
			buffer = buffer.append(key.substring(0,4)).append("年").append(key.substring(4,6)).append("月")
					 .append(key.substring(6,8)).append("日").append(key.substring(8,10)).append("点")
					 .append(key.substring(10,12)).append("分").append(key.substring(12,14)).append("秒");
			String time = buffer.toString();
			String kaluli = "";
			
			String[] temp = value.split(";");
			
			for(int i=0;i<temp.length;i++){
				String dataString = temp[i];
				String[] temp2 = dataString.split(":");
				if("Type".equals(temp2[0])){
					type = temp2[1];
				}else if("costTime".equals(temp2[0])){
					costTime = temp2[1]+"秒";
				}else if("kaluli".equals(temp2[0])){
					kaluli = temp2[1]+"卡";
				}
			}
			
			map = new HashMap<String, Object>();
			map.put("sprotTime", costTime);
			map.put("kaluli", kaluli);
			map.put("type", type);
			map.put("time", time);
			
			list.add(map);
			
		}
		//ListView listView = (ListView)findViewById(R.id.dataView);
		
		SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.datalist,
				new String[]{"type","sprotTime","kaluli","time"},
				new int[]{R.id.type,R.id.sprotTime,R.id.energy,R.id.time});
		
		setListAdapter(adapter);
	}
	
	
}
